package com.yy.permissions_general.utils;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yy.permissions_general.common.constant.StatusEnum;
import com.yy.permissions_general.module.generator.common.Pagination;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* @Description: 返回结果封装
* @Author: YY
* @Date: 2021/3/14
*/
@Data
public class ResultUtil implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private Object data;

    /**
     * 条数
     */
    private Long count;

    private ResultUtil(Boolean success, Integer code, String msg, Object data, Long count) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }
    private ResultUtil() {
    }

    /**
     * 构建LayUI数据表格分页模型
     *
     * @param page {@link IPage}
     * @return 分页对象
     */
    public static ResultUtil buildPagination(IPage page) {
        return new ResultUtil(true,StatusEnum.SUCCESS.getCode(),StatusEnum.SUCCESS.getMessage(),page.getRecords(),page.getTotal());
    }
    /**
     * 构建LayUI数据表格分页模型
     *
     * @param page {@link Pagination}
     * @return 分页对象
     */
    public static ResultUtil buildPagination(Pagination page) {
        return new ResultUtil(true,StatusEnum.SUCCESS.getCode(),StatusEnum.SUCCESS.getMessage(),page.getRows(),page.getRowTotal());
    }

    /**
     * 构建LayUI数据表格分页模型
     *
     * @param data list集合
     * @return 分页对象
     */
    public static ResultUtil buildPagination(List data) {
        return new ResultUtil(true,StatusEnum.SUCCESS.getCode(),StatusEnum.SUCCESS.getMessage(),data,Convert.toLong(data.size(), 0l));
    }

    /**
     * 成功
     * @return {ResultUtil}
     */
    public static ResultUtil success() {
        return new ResultUtil(true, StatusEnum.OK.getCode(), StatusEnum.OK.getMessage(),"",0l);
    }

    /**
     * 成功
     * @return {ResultUtil}
     */
    public static ResultUtil success(String msg) {
        return new ResultUtil(true, StatusEnum.OK.getCode(), msg, "",0l);
    }

    /**
     * 成功
     * @return {ResultUtil}
     */
    public static ResultUtil success(Object data) {
        return new ResultUtil(true, StatusEnum.OK.getCode(), StatusEnum.OK.getMessage(), data,0l);
    }

    /**
     * 成功
     * @return {ResultUtil}
     */
    public static ResultUtil success(String msg, Object data) {
        return new ResultUtil(true, StatusEnum.OK.getCode(), msg, data,0l);
    }

    /**
     * 错误
     * @return {ResultUtil}
     */
    public static ResultUtil error(StatusEnum statusEnum) {
        return new ResultUtil(false,statusEnum.getCode(), statusEnum.getMessage(), "",0l);
    }
    /**
     * 错误
     * @return {ResultUtil}
     */
    public static ResultUtil error(String msg) {
        return new ResultUtil(false, StatusEnum.INTERNAL_SERVER_ERROR.getCode(), msg, "",0l);
    }

    /**
     * 错误
     * @return {ResultUtil}
     */
    public static ResultUtil error(Integer code, String msg) {
        return new ResultUtil(false, code, msg, "",0l);
    }

    /**
     * 错误
     * @return {ResultUtil}
     */
    public static ResultUtil error(Integer code, String msg, Object data) {
        return new ResultUtil(false, code, msg, data,0l);
    }

    /**
     * 自定义提示
     * @return {ResultUtil}
     */
    public static ResultUtil msg(Boolean success, Integer code, String msg, Object data) {
        return new ResultUtil(success, code, msg, data,0l);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

}
